#@layoutMain()
#define mainMessage()

<fieldset class="layui-elem-field layui-field-title">
	<legend>
		用户管理
	</legend>
</fieldset>
<div style="padding-left: 10px;">
	<div>
		<div style="padding: 20px">
			<div class="layui-form">
				<div class="layui-inline">
					<label class="layui-form-label">用户名称：</label>
					<div class="layui-input-inline">
						<input type="text" id="s_name" name="s_name" style="width: 300px" placeholder="请输入"
						autocomplete="off" class="layui-input" onkeydown="if(event.keyCode==13) search()">
					</div>
				</div>
				&nbsp;&nbsp;
				<div class="layui-inline">
					<button class="layui-btn layuiadmin-btn-list" lay-submit="" onclick="search()"
					lay-filter="LAY-app-contlist-search">
						<i class="layui-icon layui-icon-search layuiadmin-button-btn">
						</i>
					</button>
				</div>
			</div>
		</div>

	</div>
	<div>
		<table class="layui-hide" id="userListTable"></table>
	</div>
</div>

#end

#define js()

<script type="text/html" id="vipSwitchTpl">
	<!-- 这里的 checked 的状态只是演示 -->
	<input type="checkbox" name="{{d.uid}}" value="{{d.is_vip}}" lay-skin="switch" lay-filter="vipSwitch" lay-text="&nbsp;是&nbsp;|&nbsp;否&nbsp;" {{ d.is_vip == true ? 'checked' : '' }}>
</script>

<script type="text/html" id="offSwitchTpl">
	<!-- 这里的 checked 的状态只是演示 -->
	<input type="checkbox" name="{{d.uid}}" value="{{d.is_off}}" lay-skin="switch" lay-filter="offSwitch" lay-text="&nbsp;是&nbsp;|&nbsp;否&nbsp;" {{ d.is_off == true ? 'checked' : '' }}>
</script>

<script>
layui.use(['element','form','table'], function(){
    var form=layui.form;
	  var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
	  $ = layui.jquery; // 使用jquery
	  table = layui.table;
	  
	  table.render({
	    elem: '#userListTable'
	    ,url:'/admin/user/userListTable'
	    ,id: 'userList'
	    ,cols: [[
			{field:'uid', width:80, title: '编号', sort: true,align:'center'}
	      ,{field:'username', width:150, title: '用户名称',align:'center'}
	      ,{field:'email', width:200, title: '邮箱',align:'center',sort: true}
	      ,{field:'sex', width:80, title: '性别',align:'center',sort: true}
	      ,{field:'points', width:80, title: '积分',align:'center',sort: true}
	      ,{field:'is_vip', width:110, title: '是否VIP',align:'center',sort: true,templet:vipSwitchTpl}
	      ,{field:'is_off', width:130, title: '是否封禁',align:'center',sort: true,templet:offSwitchTpl}
	      ,{field:'register_date', width:210, title: '注册日期', sort: true,align:'center'}
	      ,{field:'action', width:340, title: '操作',align:'center',templet:formatAction}
	    ]]
	    ,page: true
	  });
	  
		// 监听会员设置操作
	    form.on('switch(vipSwitch)', function(obj){
	    	$.post("/admin/user/updateVipState",{"uid":this.name,"is_vip":obj.elem.checked},function(result){
				if(result.success){
					layer.msg("执行成功！");
					table.reload("userListTable",{});
				}else{
					layer.msg("执行失败，请联系管理员！");
				}
			});
	    });
		
		 // 监听用户状态操作
	    form.on('switch(offSwitch)', function(obj){
	    	$.post("/admin/user/updateUserState",{"uid":this.name,"is_off":obj.elem.checked},function(result){
				if(result.success){
					layer.msg("执行成功！");
					table.reload("userListTable",{});
				}else{
					layer.msg("执行失败，请联系管理员！");
				}
			});
	    });
	
});
 

//删除一个用户
function deleteOne(uid){
	layer.confirm('您确定要删除这个用户吗？', {
     title:"系统提示"
	  ,btn: ['确定','取消'] //按钮
	}, function(){
		layer.closeAll('dialog');
		window.location.href="/admin/user/deleteUser/"+uid;
	}, function(){
	 
	});

}
//给用户充值积分
function addPoints(uid){
	layer.open({
		  type: 2,
		  title: '充值积分',
		  area: ['400px', '300px'],
		  content: '/admin/addPoints.html?uid='+uid //iframe的url
		}); 
}

//充值用户的密码
function resetPassword(uid){
	layer.confirm('您确定要对用户进行重置密码吗？', {
	    title:"系统提示"
		  ,btn: ['确定','取消'] //按钮
		}, function(){
			layer.closeAll('dialog');
			$.post("/admin/user/resetPassword",{"uid":uid},function(result){
				if(result.success){
					layer.msg("重置成功！");
					table.reload("userListTable",{});
				}else{
					layer.msg("重置失败，请联系管理员！");
				}
			},"json");
		}, function(){
		  
		});
}

//给用户发送私信
function addMessage(uid){
	layer.open({
		  type: 2,
		  title: '发送私信',
		  area: ['500px', '300px'],
		  content: '/admin/addMessage.html?uid='+uid //iframe的url
		}); 
}

//编辑一个文章
function modifyArticle(aid){
	//layer.alert("11");
	window.location.href="/article/toModifyArticlePage/"+aid;
}


//文章右侧的操作layui-icon layui-icon-add-1
function formatAction(d){
	return "<button class='layui-btn layui-btn-normal layui-btn-xs' onclick='resetPassword("+d.uid+")'><i class='layui-icon layui-icon-edit'></i>改密码</button>"
	+"<button class='layui-btn layui-btn-danger layui-btn-xs' onclick='addMessage("+d.uid+")'><i class='layui-icon layui-icon-notice'></i>发私信</button>"
	+"<button class='layui-btn layui-btn layui-btn-xs' onclick='addPoints("+d.uid+")'><i class='layui-icon layui-icon-add-1'></i>充积分</button>"
	+"<button class='layui-btn layui-btn-warm layui-btn-xs' onclick='deleteOne("+d.uid+")'><i class='layui-icon layui-icon-delete' ></i>删用户</button>";
}

//查询
function search(){
	var s_name=$("#s_name").val();
	table.reload("userList",{
		page:{
			curr:1
		}
		,where:{
			name:s_name
		}
	});
	
}

</script>
	
#end